package com.igoal.service.oauth;

import static net.oauth.OAuth.HMAC_SHA1;
import static net.oauth.OAuth.OAUTH_SIGNATURE_METHOD;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.oauth.OAuthAccessor;
import net.oauth.OAuthConsumer;
import net.oauth.OAuthMessage;
import net.oauth.ParameterStyle;

public class GoogleContactsService extends HttpServlet {

	private static final long serialVersionUID = 1L;

	private static final String CONSUMER_NAME = "googleContacts";

	@Override
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws IOException, ServletException {
		OAuthConsumer consumer = null;
		try {
			consumer = ContactService.getConsumer(CONSUMER_NAME,
					getServletContext());
			OAuthAccessor accessor = ContactService.getAccessor(request,
					response, consumer);

			accessor.consumer.setProperty(OAUTH_SIGNATURE_METHOD, HMAC_SHA1);
			OAuthMessage message = accessor
					.newRequestMessage(
							OAuthMessage.GET,
							"http://www.google.com/m8/feeds/contacts/default/full?max-results=1000",
							null);
			OAuthMessage result = ContactService.CLIENT.invoke(message,
					ParameterStyle.AUTHORIZATION_HEADER);

			// Handle result
			ContactService.copyResponse(result, response);
			// response.sendRedirect("result.jsp");
		} catch (Exception e) {
			ContactService.handleException(e, request, response, consumer);
		}
	}

}
